import axios from 'axios';
import type { resultType } from './type';
import { message } from 'antd';

const baseURL = "http://localhost:3001";

const http = axios.create({
  baseURL,
  timeout: 10000,
});


//请求拦截器
http.interceptors.request.use(
  (config) => {
    //携带token
    const token = localStorage.getItem('token');
    if (token) {
      config.headers.Authorization = `Bearer ${token}`;
    }
    return config;
  },
  (error) => {
    
    return Promise.reject(error);
  },
);


//响应拦截器
http.interceptors.response.use((res)=>{
  const resData = (res.data || {}) as resultType
  const { errno, data, msg } = resData;
  if (errno !== 0) {
    //错误提示
   if(msg) {
     message.error(msg);
   }
   throw new Error(msg);
  }
  

  return data as any;

});


export default http;